package com.qztc.controller;

import com.qztc.hire_end_dto.dto.NoticeType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static com.qztc.config.GlobalConfig.SERVICE_URL;

@RestController
@RequestMapping("/consumer/noticeType")
@Tag(name = "消费者端公告类型管理接口")
public class NoticeTypeControllerByRestTemplate {
    @Autowired
    private RestTemplate restTemplate;

    @Operation(summary = "查询所有公告类型")
    @GetMapping("/getAll")
    public String getAll() {
        return restTemplate.getForObject(SERVICE_URL + "/noticeType/getAll", String.class);
    }

    @Operation(summary = "查询公告类型")
    @GetMapping("/getbynoticeTypeId")
    public String getbynoticeTypeId(@RequestParam Integer noticeTypeId) {
        return restTemplate.getForObject(SERVICE_URL + "/noticeType/getbynoticeTypeId?noticeTypeId="+noticeTypeId, String.class);
    }

    @Operation(summary = "修改公告类型")
    @PutMapping("/update")
    public String update(@RequestBody NoticeType noticeType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<NoticeType> entity = new HttpEntity<>(noticeType, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL + "/noticeType/update", HttpMethod.PUT,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "删除公告类型")
    @DeleteMapping("/delete")
    public String delete(@RequestBody NoticeType noticeType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<NoticeType> entity = new HttpEntity<>(noticeType, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL + "/noticeType/delete", HttpMethod.DELETE, entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "增加公告类型")
    @PostMapping("/add")
    public String add(@RequestBody NoticeType noticeType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<NoticeType> entity = new HttpEntity<>(noticeType, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL + "/noticeType/add", HttpMethod.POST, entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "批量删除公告类型")
    @DeleteMapping("/deleteMany")
    public String deleteMany(@RequestBody List<Integer> noticeTypeIds) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Integer>> entity = new HttpEntity<>(noticeTypeIds, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL + "/noticeType/deleteMany", HttpMethod.DELETE, entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "批量增加公告类型")
    @PostMapping("/addMany")
    public String addMany(@RequestBody List<NoticeType> noticeTypes) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<NoticeType>> entity = new HttpEntity<>(noticeTypes, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL + "/noticeType/addMany", HttpMethod.POST, entity, String.class);
        return result.getBody();
    }
}
